package sparrowGui
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import sparrowGui.event.AlertEvent;
	import sparrowGui.item.LabelItem;
	import sparrowGui.skin.AlertSkin;
	import sparrowGui.utils.PubFun;
	
	[Event(name="alert_close", 	type="sparrowGui.event.AlertEvent")]
	[Event(name="alert_update", 	type="sparrowGui.event.AlertEvent")]
	/**
	 * 警告窗
	 * @author Pelephone
	 */
	public class Alert extends Component
	{
		/**
		 * 按钮前缀
		 */
		private static const BTN_PREFIX:String = "btn_prefix$";
//		private var _btnCls:Class;
		
		// 半透明背景
		private var translucent:Sprite;
		/**
		 * 父级容器 
		 */
		private var parentSkin:DisplayObjectContainer;
		
		/**
		 * 缓存初始项	-1,不缓存; 0,自动,跟椐最大数据量缓存; >0,跟椐指定条数缓存
		 */
		private var itemCache:int;
		
		private var _spacing:int = 2;
		private var _padding:int = 5;
		
		private var _autoClose:Boolean = true;		//是否自动关闭。
		
		/**
		 * item对象
		 */
		private var itemHash:Object;
		
		private var _itemClass:Class;
//		private var _itemSkinClass:Class;
		
		/**
		 * 构造提示窗
		 * @param argSkin
		 */
		public function Alert(argSkin:Sprite=null,parent:DisplayObjectContainer=null,itmCls:Class=null)
		{
			parentSkin = parent || SparrowGUI.mainDisp;
			itemClass = itmCls;
			translucent = new Sprite();
			create(argSkin);
			setTranslucent();
			addListener();
		}
		
		protected function create(argSkin:Sprite):void
		{
			itemHash = {};
			argSkin = argSkin || new AlertSkin();
			if(_itemClass==null) _itemClass = LabelItem;
			
			_skin = argSkin;
//			if(_itemSkinClass==null) _itemSkinClass = SkinStyleSheet.getIns().alertBtnSkinCls;
		}
		
		/**
		 * 要想新皮肤重新初始设本组件
		 * @param argSkin
		 * @param parent
		 */
		public function reCreate(argSkin:Sprite=null,parent:DisplayObjectContainer=null):void
		{
			dispose();
			if(parent) parentSkin = parent;
			create(argSkin);
			addListener();
		}
		
		public function addListener():void
		{
			skin.addEventListener(MouseEvent.CLICK,onBtnClick);
		}
		
		public function removeListener():void
		{
			skin.removeEventListener(MouseEvent.CLICK,onBtnClick);
		}
		
		/**
		 * 监听鼠标事件
		 */
		private function onBtnClick(e:MouseEvent):void
		{
			var btnName:String = e.target.name;
			
			if(btnName.indexOf(BTN_PREFIX)==0){
				var btnIndex:int = int(btnName.replace(BTN_PREFIX,""));
				if(skin.stage) skin.stage.focus = skin.stage;
				var inpTxt:String = (txtInput)?txtInput.text:null;
				
				if(_autoClose) close();
				dispatchEvent(new AlertEvent(AlertEvent.ALERT_CLOSE,btnIndex,inpTxt));
//				if(!_autoClose && _autoHidden) close();
			}
		}
		
		/**
		 * 更新
		 * @param data
		 */
		public function update(data:Object):void
		{
			if(txtAlert)
				txtAlert.text = String(data);
			dispatchEvent(new AlertEvent(AlertEvent.ALERT_UPDATE));
		}
		
		/**
		 * 弹出警告窗
		 * @param alertTxt
		 * @param btnStr
		 * @param backFun
		 */
		public function alert(alertTxt:Object,btnStr:String="确定",backFun:Function=null):void
		{
			createBtns(btnStr);
			show();
			update(alertTxt);
			
			if(backFun==null) return;
			addEventListener(AlertEvent.ALERT_CLOSE,backFun);
		}
		
		/**
		 * 将皮肤居中显示
		 */
		protected function show():void
		{
			translucent.width = SparrowGUI.stageWidth;
			translucent.height = SparrowGUI.stageHeight;
			parentSkin.addChild(translucent);
			skin.x = SparrowGUI.stageWidth/2 - skin.width/2;
			skin.y = SparrowGUI.stageHeight/2 - skin.height/2;
			parentSkin.addChild(skin);
		}
		
		/**
		 * 通过字符数据生成按钮
		 * @param btnStr		用竖线分隔的按钮字"确定|取消"
		 * @param btnCla
		 */
		protected function createBtns(btnStr:String="确定"):void
		{
			var btnArr:Array = btnStr.split("|");
			for (var i:int = 0; i < btnArr.length; i++) 
			{
				var itm:DisplayObject = createBtn(btnArr[i],i);
				if(!posBtn) continue;
				
				var lx:int = itm.width*btnArr.length/2 + spacing*(btnArr.length-1)/2;
				itm.x = posBtn.x - lx + (itm.width+spacing)*i;
				itm.y = posBtn.y - itm.height;//height  - padding;
			}
		}
		
		/**
		 * 从缓存链表中生成item
		 * @return 
		 */
		protected function createBtn(btnStr:String="确定",id:int=0):DisplayObject
		{
			var itm:DisplayObject = new _itemClass;
//			var itmSkin:Sprite;
			
			if(!posBtn)
				itm = skin.getChildByName(BTN_PREFIX + id);
			else
			{
				itm = new _itemClass();
				itm.name = BTN_PREFIX + id;
			}
			
			if(!itm) return null;
			
			if(itm.hasOwnProperty("update"))
				(itm["update"] as Function).apply(null,[btnStr]);
			
			PubFun.setSimpleButtonText(itm,btnStr);
			
			itemHash[itm.name] = itm;
			
			if(posBtn)
				skin.addChild(itm);
			
			return itm;
		}
		
		/**
		 * 跟dispose不同，close只是关掉，并不断开引用
		 */
		public function close():void
		{
			if(translucent.parent)
				translucent.parent.removeChild(translucent);
			if(skin.parent)
				skin.parent.removeChild(skin);
			
			for each (var itm:DisplayObject in itemHash) 
			{
				if(itm.hasOwnProperty(dispose))
					(itm["dispose"] as Function).apply(null,null);
				if(!posBtn) continue;
				skin.removeChild(itm);
			}
			itemHash = {};
		}
		
		/**
		 * 断开引用并回收
		 */
		override public function dispose():void
		{
			close();
			super.dispose();
		}
		
		/**
		 * 设置半透明
		 * @param color
		 * @param alphpa
		 * @param rect
		 */
		public function setTranslucent(color:int=0x000000,talphpa:Number=0.3,rect:Rectangle=null):void
		{
			translucent.graphics.clear();
			translucent.graphics.beginFill(color,talphpa);
			
			if(rect)
				translucent.graphics.drawRect(rect.x,rect.y,rect.width,rect.height);
			else
				translucent.graphics.drawRect(0,0,SparrowGUI.stageWidth,SparrowGUI.stageHeight);
			
			translucent.graphics.endFill();
		}
		
		////////////////////////////////////
		// get/set
		///////////////////////////////////
		
		/**
		 * 子项的Class,必须继承Item
		 * 不想用默认皮肤,可以先设置此基再update数据
		 */
		public function get itemClass():Class
		{
			return _itemClass;
		}
		
		/**
		 * @private
		 */
		public function set itemClass(value:Class):void
		{
			_itemClass = value;
		}
		
		/**
		 * 子项皮肤类,必须是是Sprite显示对象
		 * 不想用默认皮肤,可以先设置此基再update数据
		 
		public function get itemSkinClass():Class
		{
			return _itemSkinClass;
		}*/
		
		/**
		 * @private
		 
		public function set itemSkinClass(value:Class):void
		{
			_itemSkinClass = value;
		}*/
		

		/**
		 * 边距
		 */
		public function get padding():int
		{
			return _padding;
		}

		/**
		 * @private
		 */
		public function set padding(value:int):void
		{
			_padding = value;
		}

		/**
		 * 按钮间距
		 */
		public function get spacing():int
		{
			return _spacing;
		}
		
		/**
		 * 警告窗是否显示
		 */
		public function get isShow():Boolean 
		{
			return skin.parent!=null;
		}

		/**
		 * @private
		 */
		public function set spacing(value:int):void
		{
			_spacing = value;
		}

		public function get autoClose():Boolean
		{
			return _autoClose;
		}

		public function set autoClose(value:Boolean):void
		{
			_autoClose = value;
		}
		
		/**
		 * 按钮类
		 
		public function get btnCls():Class
		{
			return _btnCls;
		}*/
		
		/**
		 * @private
		 
		public function set btnCls(value:Class):void
		{
			_btnCls = value;
		}*/
		
		//提示窗文字文本
		public function get txtAlert():TextField
		{return skin["txt_alert"] as TextField;}
		//提示输入文本
		public function get txtInput():TextField
		{return skin["txt_input"] as TextField;}
		//动态按钮坐标
		public function get posBtn():Sprite
		{return skin.getChildByName("posBtn") as Sprite;}
	}
}
